//
//  ViewController.m
//  BMW
//
//  Created by up72-2 on 15/10/26.
//  Copyright © 2015年 up72-2. All rights reserved.
//

#import "ViewController.h"
#import "NetManager.h"
#import "NetManager+UPRequest.h"
#import "MainHomeVC.h"
@interface ViewController ()<NetManagerDelegate>
{
    NetManager *netManager;
}
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UILabel *versionLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self allocNet];
    
    //初始换ui风格
    [self allocUIStyle];
    
    //添加释放键盘的通知
    [self addKeyBoardGesture];
    
    //获取版本号
    [self getVersions];
    
    //键盘通知
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.nameTF.text = @"";
    self.pwdTF.text = @"";
}


- (void)allocNet
{
    netManager = [[NetManager alloc]init];
    netManager.delegate = self;
}

- (void)allocUIStyle
{
    self.pwdTF.secureTextEntry = YES;
    
    self.loginBtn.layer.cornerRadius = 4;
    self.loginBtn.layer.masksToBounds = YES;
    
    self.loginBtn.backgroundColor = RGBACOLOR(2, 146, 206, 1);
}

- (void)addKeyBoardGesture
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didtap:)];
    tap.numberOfTouchesRequired =1;
    tap.numberOfTapsRequired =1;
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
}

- (void)getVersions
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    self.versionLabel.text = app_Version;

//    NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
}

-(void)didtap:(UITapGestureRecognizer *)didpap
{
    //讲UITableView设置成不可用
    [self.view endEditing:YES];
    [self moveFrame:0];
    
}


- (IBAction)login:(UIButton *)sender
{
#warning 需测试接口
    if ([self.nameTF.text isEqualToString:@""]) {
        [MTool showMessage:@"请输入用户名" inView:self.view];
    }
    else if([self.pwdTF.text isEqualToString:@""])
    {
        [MTool showMessage:@"请输入密码" inView:self.view];
    }
    else
    {
        netManager.loadingText = @"登录中";
        [netManager login:self.nameTF.text pwd:self.pwdTF.text];
    }
    
//    MainHomeVC *homeVC = [[MainHomeVC alloc]init];
//    [self.navigationController pushViewController:homeVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    //调起键盘
    
}

#pragma mark - 键盘代理
- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary* info = [notification userInfo];
    //kbSize即為鍵盤尺寸 (有width, height)
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
    
    offY = kbSize.height - 66;
    [self moveFrame:offY];
}

- (void)moveFrame:(CGFloat)offy {
    
    //当出现键盘时，添加frame的高度，已便键盘的使用
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    frame.origin.y = -offy;
    [self.view setFrame:frame];
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
    [UIView commitAnimations];
    
}

#pragma mark - 网络请求
- (void)requestStart:(NetManager *)request
{
}

- (void)requestError:(NetManager *)request error:(NSError *)error
{
    
}

- (void)requestDidFinished:(NetManager *)request result:(NSMutableDictionary *)result
{
    NSInteger status = [[NSString stringWithFormat:@"%@",[result objectForKey:@"state"]] integerValue];
    
    if (status == 0) {
        [MTool showMessage:[NSString stringWithFormat:@"%@",[result objectForKey:@"errorMsg"]] inView:self.view];
        return;
    }
    
    [MTool storeCurrentUserName:self.nameTF.text];
    [MTool storeCurrentData:[result objectForKey:@"data"]];
    
    MainHomeVC *homeVC = [[MainHomeVC alloc]init];
    [self.navigationController pushViewController:homeVC animated:YES];
}
@end
